#include "key_input_task.h"
#include "relay_output.h"
#include "iotgo_service.h"
#include "platformAPI.h"

#define DEBUG_TASK_KEY 1

#ifdef DEBUG_TASK_KEY
#include <stdio.h>
#endif

#define tskKey_PRIORITY (tskIDLE_PRIORITY + 3)
#define KEY_UP   1
#define KEY_DOWN 0

static xTaskHandle keytask;
static uint8 key_count;
static bool  key_state;
static bool  key_pre_state;
static uint8 short_press_message = 0;
static uint8 long_press_message = 0;

static void keyScan(void)
{
    uint8 key_up_flag = 0;

    /* get current key state*/
    key_state = GPIO_INPUT_GET(GPIO_ID_PIN(0));
    /* key is pressed at current time*/
    if(key_state == KEY_DOWN)
    {
	/* first pressed*/
	if(key_pre_state == KEY_UP)
	{
	    key_count = 0;
	}
	else
	{
	    key_count++;
	}
		
    }
    else /* key is up at current time*/
    {
	key_up_flag = 1;
    }

    /***********************************
     * short press is:
     * 1. current key state is up
     * 2. pressed state time is >=  60ms
     * 4. pressed state time is < 1s
     ***********************************/
    if(key_up_flag == 1 &&
       key_count > 2 &&
       key_count < 50)
    {
	short_press_message++;
        key_count = 0;
    }
    /*********************************
     * long press: pressed time >= 4s
     *********************************/
    else if(key_count == 200)
    {
	long_press_message++;
	key_count = 51;
    }
    else
    {
	/*pressed time is [1s ~ 4s]*/
	/*do nothing */
    }
	
    /* clear state*/
    key_pre_state = key_state;
}

static void keyMessageHandle(void)
{
    uint8 iotgo_state = IOTGO_IDLE;
    /*check key messages*/
    if(short_press_message > 0)
    {
	printf("key short pressed\n");
	iotgo_state = iotgoGetState();
	if(iotgo_state < IOTGO_PAIR_WITH_APP)
	{
	    relayUpdateByLocal();
	}
	else if(iotgo_state == IOTGO_PAIR_WITH_APP)
	{
	    iotgoPairWithAppStop();
	}
	short_press_message--;
    }
	
    if(long_press_message > 0)
    {
	printf("key long pressed\n");
	iotgo_state = iotgoGetState();
	if(iotgo_state < IOTGO_PAIR_WITH_APP)		
	{
	    iotgoPairWithAppStart();
	}
	long_press_message--;
    }
}

static void keyThread(void *parameter)
{
    while(1)
    {
        keyScan();
        keyMessageHandle();
        systemDelayMs(20);
    }
}

int startKeyInputTask(void)
{
    char ret = -1;
    uint8 i = 0;
    /*step1 : init the gpio*/
    PIN_FUNC_SELECT(GPIO_PIN_REG(0),FUNC_GPIO0);
    GPIO_DIS_OUTPUT(GPIO_ID_PIN(0));

    key_state = KEY_UP;
    key_pre_state = KEY_UP;
    short_press_message = 0;
    long_press_message = 0;
    key_count = 0;

    /*step2 : create the task*/
    if(xTaskCreate(keyThread, 
		   ((const char*)"keyTread"), 
		   500, 
		   NULL,
		   tskKey_PRIORITY,
		   &keytask) != pdPASS)
    {	
        printf("create connect_servers_threads failed\n");
	return -1;
    }
    return 0;
}

int stopKeyInputTask(void)
{
    vTaskDelete(keytask);
    return 0;
}


